package com.example.expresscabinet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.expresscabinet.dao.getexpressMapper;
import com.example.expresscabinet.model.getexpress;
import com.example.expresscabinet.service.ICabinetService;
import com.example.expresscabinet.service.IFriendService;
import com.example.expresscabinet.service.IGetexpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Service
public class getexpressServiceImpl extends ServiceImpl<getexpressMapper, getexpress> implements IGetexpressService {

    @Resource
    getexpressMapper getexpressMapper;

    @Autowired
    ICabinetService cabinetService;

    @Autowired
    IFriendService friendService;

    //添加getexpress
    @Override
    public void addGetexpress(String tel,int num,String expresstel){
        getexpress ge=new getexpress();
        ge.setCode((int)((Math.random()*9+1)*100000)+"");
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        ge.setIntime(nousedate);
        ge.setNum(num);
        ge.setTel(tel);
        ge.setStates("未取走");
        ge.setExpresstel(expresstel);
        getexpressMapper.insert(ge);
        cabinetService.takeOccupyCabinet(num);
    }


    //修改getexpress
    @Override
    public int takeGetexpress(String code){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("code",code);
        getexpress ge=new getexpress();
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        ge.setOuttime(nousedate);
        getexpress bge=getexpressMapper.selectOne(wrapper);
        Timestamp beforetime=null;
        if(bge!=null){
            beforetime=bge.getIntime();
        }
        long t1=nousedate.getTime();
        long t2=beforetime.getTime();
        int hours=(int)((t1-t2)/(1000*60*60));
        int cost=hours<=24?0:(hours-24);
        ge.setCost(cost);
        getexpressMapper.updateStates(code,cost,nousedate);
        return cost;
    }

    @Override
    public int queryCost(String code){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("code",code);
        getexpress ge=new getexpress();
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        ge.setOuttime(nousedate);
        getexpress bge=getexpressMapper.selectOne(wrapper);
        Timestamp beforetime=null;
        if(bge!=null){
            beforetime=bge.getIntime();
        }
        long t1=nousedate.getTime();
        long t2=beforetime.getTime();
        int hours=(int)((t1-t2)/(1000*60*60));
        int cost=hours<=24?0:(hours-24);
        return cost;
    }

    @Override
    public Page<getexpress> querynotake(String tel,int pageNum,int pageSize){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel).eq("states","未取走");
        Page<getexpress> res=getexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

    @Override
    public Page<getexpress> querytake(String tel,int pageNum,int pageSize){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel).eq("states","已取走");
        Page<getexpress> res=getexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

    @Override
    public Page<getexpress> querynotakebyexpressman(String expresstel,int pageNum,int pageSize){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("expresstel",expresstel).eq("states","未取走");
        Page<getexpress> res=getexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

    @Override
    public Page<getexpress> querytakebyexpressman(String expresstel,int pageNum,int pageSize){
        QueryWrapper<getexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("expresstel",expresstel).eq("states","已取走");
        Page<getexpress> res=getexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }
}
